/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exception.DAOCandidatException;
import dao.exception.DAOFormationException;
import dao.interfaces.DAOFormation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Candidat;
import model.Formation;

/**
 *
 * @author Paul
 */
public class DAOFormationImpl implements DAOFormation {

    private DAOInit daoConnection;

    public DAOFormationImpl(DAOInit daoConnection) {
        this.daoConnection = daoConnection;
    }

    @Override
    public ArrayList<Formation> findAll() throws DAOFormationException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Formation findFormationFromCandidat(int idCandidat) throws DAOFormationException {
        String query = "SELECT f.idFormation, f.nomFormation FROM formation f, formation_candidat_asso fca "
                + "where f.idFormtion = fca.idFormation AND fca.idCandidat = ?";
        Formation formation = null;
        try {
            Connection conn = daoConnection.getConnection();//Creation de la connexion
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setInt(1, idCandidat);
            ResultSet res = stmt.executeQuery(query);
            while (res.next()) {
                int idFormation = res.getInt("idFormation");
                String nomFormation = res.getString("nomFormation");
                formation = new Formation(idFormation, nomFormation);
            }
            if (formation == null) {
                throw new DAOFormationException("Erreur de récupération de la formation du candidat : veuillez contacter avec l'administrateur du site");
            }
            res.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return formation;
    }

    @Override
    public void create(Formation formation) throws DAOFormationException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void update(Formation formation) throws DAOFormationException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(int idFormation) throws DAOFormationException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
